package com.zy.qudadid.ui.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zy.base.AESCrypt;
import com.zy.lcpassenger.R;
import com.zy.qudadid.model.ReportTitle;
import com.zy.qudadid.model.StrokeInfor;
import com.zy.qudadid.network.Const;
import com.zy.qudadid.ui.view.base.OnItemClickListener;
import com.zy.qudadid.ui.widget.ListViewDialog;
import com.zy.qudadid.ui.widget.OneButtonReportDialog;
import com.zy.qudadid.utils.TimeUtil;
import com.zy.qudadid.utils.ToolsUtil;
import com.zy.util.JsonUtils;
import com.zy.util.UserUtil;
import com.zy.util.Util;
//import com.zhy.http.okhttp.OkHttpUtils;
//import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by ninos on 2016/9/20.
 */
public class IndexCommonAdapter extends RecyclerView.Adapter<IndexCommonAdapter.VHolder> {

    ArrayList<StrokeInfor> data = new ArrayList<>();
    Context context;
    StrokeInfor stroke;

    public IndexCommonAdapter(Context context, ArrayList<StrokeInfor> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public VHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        VHolder holder = new VHolder(LayoutInflater.from(
                context).inflate(R.layout.activity_index_common_item, parent,
                false));
        return holder;
    }

    @Override
    public int getItemCount()
    {
        return data.size();
    }

    // 举报标题
    ArrayList<ReportTitle> listReport = new ArrayList<ReportTitle>();

    @Override
    public void onBindViewHolder(VHolder holder, final int position)
    {
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onItemClick(view,  position , data.get(position));
                }
            });

        } else {
            holder.itemView.setOnClickListener(null);
        }

        stroke =(StrokeInfor) data.get(position);
        String date = stroke.yuyueche_departtime;
        String[] str = date.split(" ");
        date = TimeUtil.getDay(str[0]);
        if(stroke.state.equals("4")) { // 已取消
            holder.txtDate.setText("已取消");
            holder.txtDate.setBackgroundResource(R.drawable.border_deepgray_4dp);
            holder.txtReport.setVisibility(View.VISIBLE);
            holder.txtReport.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final OneButtonReportDialog dialog1 = new OneButtonReportDialog(context);
                    dialog1.txt_content.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Map<String,String> map = new HashMap<String, String>();
                            map.put("type","2");
                            String json = JsonUtils.toJson(map);
                            AESCrypt aesCrypt = null;
                            try {
                                aesCrypt = new AESCrypt();
                                String d = Util.replaceBlank(aesCrypt.encrypt(json));
                                OkHttpUtils.post()
                                        .url(Const.BASE_URL+"api.php/Order/dictionaries_title")
                                        .addParams("args",d)
                                        .build()
                                        .execute(new StringCallback() {
                                            @Override
                                            public void onError(Call call, Exception e, int i) {

                                            }

                                            @Override
                                            public void onResponse(String response,int i) {
                                                ToolsUtil.print("----","获取订单标题返回值："+response);
                                                JSONObject json;
                                                try {
                                                    json = new JSONObject(response);
                                                    int code = json.getInt("code");
                                                    if(code == 200){
                                                        JSONArray datas = json.getJSONArray("datas");
                                                        listReport.clear();
                                                        listReport = new Gson().fromJson(datas.toString(),new TypeToken<ArrayList<ReportTitle>>(){}.getType());

                                                        final ListViewDialog dialog2 = new ListViewDialog(context);
                                                        ReportTitleAdapter adapter = new ReportTitleAdapter(context,listReport);
                                                        dialog2.listview.setAdapter(adapter);

                                                        dialog2.listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                                            @Override
                                                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                                                dialog1.txt_content.setText(listReport.get(position).getTitle());
                                                                dialog2.dismiss();
                                                            }
                                                        });

                                                    }
                                                } catch (JSONException e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        });
                            } catch (Exception e) {
                                e.printStackTrace();
                            }


                        }
                    });
                    dialog1.txt_queding.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            String title = dialog1.txt_content.getText().toString();
                            String content = dialog1.et_content.getText().toString();
                            Map<String,String> map = new HashMap<String, String>();
                            map.put("user_id",new UserUtil(context).getUserId());
                            map.put("complain_id",stroke.driver_user_id);
                            map.put("title",title);
                            map.put("content",content);
                            map.put("order_id",stroke.id);
                            map.put("type","1");
                            String json = JsonUtils.toJson(map);
                            AESCrypt aesCrypt = null;
                            try {
                                aesCrypt = new AESCrypt();
                                String d = Util.replaceBlank(aesCrypt.encrypt(json));
                                OkHttpUtils.post()
                                        .url(Const.BASE_URL+"api.php/Order/complain")
                                        .addParams("args",d)
                                        .build()
                                        .execute(new StringCallback() {
                                            @Override
                                            public void onError(Call call,Exception e,int i) {

                                            }

                                            @Override
                                            public void onResponse(String response,int i) {
                                                JSONObject json;
                                                try {
                                                    json = new JSONObject(response);
                                                    int code = json.getInt("code");
                                                    if(code == 200){
                                                        Toast.makeText(context,"投诉成功",Toast.LENGTH_SHORT).show();
                                                        dialog1.dismiss();
                                                    }
                                                } catch (JSONException e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        });
                            }catch (Exception e){

                            }

                        }
                    });
                }
            });
        }else if(stroke.state.equals("3")){ // 已完成
            holder.txtDate.setText("已完成");
            holder.txtDate.setBackgroundResource(R.drawable.border_deepgreen_4dp);
            holder.txtReport.setVisibility(View.GONE);
        }
        holder.ixi_time.setText(str[1].substring(0,str[1].lastIndexOf(":")));
        holder.ici_adds.setText("到达地："+stroke.destination);
    }


    class VHolder extends RecyclerView.ViewHolder {

        @Nullable @Bind(R.id.ici_adds)TextView ici_adds;
        @Nullable @Bind(R.id.ixi_time)TextView ixi_time;
        @Nullable @Bind(R.id.txtDate) TextView txtDate;
        @Nullable @Bind(R.id.txtReport) TextView txtReport;

        VHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    /**
     * Item点击事件
     */
    public OnItemClickListener mOnItemClickListener;
    /**
     * 设置监听者
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}